package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import linter.linterjava.POJO.DO.Article;
import linter.linterjava.POJO.DO.ArticleCollect;
import linter.linterjava.POJO.DO.Collect;
import linter.linterjava.POJO.DO.NoteKnowledgebase;
import linter.linterjava.POJO.DTO.ArticleCollectDTO;
import linter.linterjava.POJO.DTO.CollectDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.custom.ConstantPublicWay;
import linter.linterjava.mapper.ArticleCollectMapper;
import linter.linterjava.mapper.ArticleMapper;
import linter.linterjava.service.CollectService;
import linter.linterjava.mapper.CollectMapper;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static linter.linterjava.custom.ConstantPublicWay.getPageInfo;
import static linter.linterjava.custom.CustomResultType.*;

/**
* @author 北煜
* @description 针对表【collect】的数据库操作Service实现
* @createDate 2024-07-11 15:43:47
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
    implements CollectService{

    @Resource
    ArticleMapper articleMapper;
    @Resource
    CollectMapper collectMapper;
    @Resource
    ArticleCollectMapper articleCollectMapper;

    @Override
    @CachePut(value = "collect", key = "{ 'collect_' + T(String).valueOf(#uuid) }")
    public String create(Long uuid, CollectDTO collectDTO) {
        Date date = new Date();
        Collect collect = new Collect();
        collect.setUserId(uuid);
        collect.setCollectName(collectDTO.getCollectName());
        collect.setCollectIntro(collectDTO.getCollectIntro());
        collect.setCreateTime(date);
        collect.setUpdataTime(date);
        int flag = collectMapper.insert(collect);
        return flag > 0 ? CREATION_SUCCESS : CREATION_ERROR;
    }

    @Override
    @CachePut(value = "collect", key = "{ 'collect_' + T(String).valueOf(#uuid) }")
    public String delete(Long uuid, Long collectId) {
        // 先进行查询看看里面有哪些文章 有文章的把文章的收藏-1
        Date date = new Date();
        Article article = new Article();
        Collect collect = new Collect();
        collect.setIsDelete(1);
        List<Long> articleIds = articleCollectMapper.selectList(new QueryWrapper<ArticleCollect>()
                        .eq("collect_id", collectId)).stream()
                .map(ArticleCollect::getCollectId).collect(Collectors.toList());
        Integer collectNub = articleMapper.selectById(collectId).getCollectNub();
        article.setCollectNub(collectNub - 1);
        article.setUpdataTime(date);
        int flag = articleMapper.update(article, new QueryWrapper<Article>()
                .in("id", articleIds).eq("is_delete", 0));
        flag += collectMapper.update(collect, new QueryWrapper<Collect>().eq("user_id", uuid).eq("id", collectId));
        return flag == 2 ? UPDATE_SUCCESS : UPDATE_ERROR;
    }

    @Override
    public PageInfo<Collect> getCollect(Long uuid, PageInfoDTO pageInfoDTO) {
        return getPageInfo(pageInfoDTO, new QueryWrapper<Collect>()
                .eq("user_id", uuid).eq("is_delete",0), collectMapper);
    }



}




